<template>
  <div class="checkout-page">
    <van-nav-bar title="结算订单" left-text="返回" left-arrow @click-left="$router.back()" />
    
    <!-- 收货地址选择 -->
    <div class="address-section" @click="goToAddressSelect">
      <div v-if="selectedAddress" class="address-info">
        <div class="address-header">
          <span class="name">{{ selectedAddress.name }}</span>
          <span class="phone">{{ selectedAddress.tel }}</span>
          <van-tag v-if="selectedAddress.isDefault" type="danger">默认</van-tag>
        </div>
        <div class="address-detail">
          {{ addressStore.fullAddress(selectedAddress) }}
        </div>
      </div>
      <div v-else class="add-address">
        <van-icon name="plus" size="18" />
        <span>选择收货地址</span>
      </div>
      <van-icon name="arrow" size="16" />
    </div>
    
    <!-- 商品列表 -->
    <div class="goods-section">
      <div class="section-title">商品清单</div>
      <div class="goods-list">
        <div v-for="(item, index) in cartItems" :key="index" class="goods-item">
          <img :src="item.picUrl || item.gallery[0]" class="goods-image" />
          <div class="goods-info">
            <div class="goods-name">{{ item.name }}</div>
            <div class="goods-specs" v-if="item.selectedSpecs && Object.keys(item.selectedSpecs).length > 0">
              <span v-for="(value, key) in item.selectedSpecs" :key="key">
                {{ key }}: {{ value }}
              </span>
            </div>
            <div class="goods-price">
              <span class="price">¥{{ (item.retailPrice * item.quantity).toFixed(2) }}</span>
              <span class="quantity">×{{ item.quantity }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 费用明细 -->
    <div class="price-section">
      <div class="section-title">费用明细</div>
      <div class="price-item">
        <span class="label">商品金额</span>
        <span class="value">¥{{ totalPrice.toFixed(2) }}</span>
      </div>
      <div class="price-item">
        <span class="label">运费</span>
        <span class="value">¥0.00</span>
      </div>
      <div class="price-total">
        <span class="label">实付金额</span>
        <span class="value">¥{{ totalPrice.toFixed(2) }}</span>
      </div>
    </div>
    
    <!-- 结算按钮 -->
    <div class="submit-bar">
      <div class="total-amount">
        合计: <span class="amount">¥{{ totalPrice.toFixed(2) }}</span>
      </div>
      <van-button 
        type="danger" 
        round 
        class="submit-btn"
        @click="submitOrder"
        :disabled="!selectedAddress || submitting"
        :loading="submitting"
      >
        {{ submitting ? '提交中...' : '提交订单' }}
      </van-button>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useCartStore } from '@/stores/cart'
import { useAddressStore } from '@/stores/address'
import { showToast } from 'vant'
import 'vant/es/toast/style'

const router = useRouter()
const cartStore = useCartStore()
const addressStore = useAddressStore()
const submitting = ref(false)

const cartItems = computed(() => cartStore.checkedItems)
const selectedAddress = computed(() => addressStore.selectedAddress)
const totalPrice = computed(() => cartStore.totalPrice)

const goToAddressSelect = () => {
  router.push('/address')
}

const submitOrder = async () => {
  if (!selectedAddress.value) {
    showToast('请选择收货地址')
    return
  }

  submitting.value = true
  
  try {
    const orderData = {
      items: cartItems.value,
      address: selectedAddress.value,
      totalPrice: totalPrice.value
    }
    await new Promise(resolve => setTimeout(resolve, 1000))
    showToast({
      message: '订单提交成功',
      icon: 'success',
      duration: 1500
    })

    cartStore.clearCheckedItems()
    setTimeout(() => {
      router.push('/order/success')
    }, 1500)
    
  } catch (error) {
    showToast('订单提交失败')
    console.error(error)
  } finally {
    submitting.value = false
  }
}

onMounted(() => {
  if (cartItems.value.length === 0) {
    showToast('请先选择商品')
    router.back()
  }
})
</script>

<style scoped>
.checkout-page {
  padding-bottom: 100px;
  background-color: #fafafa;
  min-height: 100vh;
  min-width: 78vh;
}

.address-section {
  display: flex;
  align-items: center;
  padding: 16px 12px;
  margin: 10px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.05);
  cursor: pointer;
}

.address-info {
  flex: 1;
  padding-right: 15px;
}

.address-header {
  display: flex;
  align-items: center;
  margin-bottom: 6px;
}

.address-header .name {
  font-size: 15px;
  font-weight: 500;
  margin-right: 10px;
  color: #333;
}

.address-header .phone {
  font-size: 13px;
  color: #666;
  margin-right: 8px;
}

.address-detail {
  font-size: 13px;
  color: #666;
  line-height: 1.5;
}

.add-address {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #1989fa;
  font-size: 14px;
}

.add-address .van-icon {
  margin-right: 5px;
}

.goods-section {
  margin: 12px 10px;
  background: #fff;
  border-radius: 8px;
  padding: 16px 12px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}

.section-title {
  font-size: 15px;
  font-weight: 500;
  color: #333;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid #f5f5f5;
}

.goods-list {
  margin-top: 8px;
}

.goods-item {
  display: flex;
  margin-bottom: 16px;
}

.goods-item:last-child {
  margin-bottom: 0;
}

.goods-image {
  width: 80px;
  height: 80px;
  border-radius: 6px;
  margin-right: 12px;
  object-fit: cover;
}

.goods-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.goods-name {
  font-size: 14px;
  color: #333;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}

.goods-specs {
  font-size: 12px;
  color: #999;
  margin: 6px 0;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.goods-specs span {
  background: #f7f7f7;
  padding: 2px 6px;
  border-radius: 3px;
  line-height: 1.4;
}

.goods-price {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.goods-price .price {
  font-size: 14px;
  color: #ee0a24;
  font-weight: 500;
}

.goods-price .quantity {
  font-size: 12px;
  color: #999;
}

.price-section {
  margin: 12px 10px;
  background: #fff;
  border-radius: 8px;
  padding: 16px 12px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}

.price-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}

.price-item .label {
  font-size: 13px;
  color: #666;
}

.price-item .value {
  font-size: 13px;
  color: #333;
}

.price-total {
  display: flex;
  justify-content: space-between;
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid #f5f5f5;
}

.price-total .label {
  font-size: 14px;
  color: #333;
  font-weight: 500;
}

.price-total .value {
  font-size: 15px;
  color: #ee0a24;
  font-weight: 500;
}

.submit-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50px;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  box-shadow: 0 -2px 8px rgba(0,0,0,0.1);
  z-index: 100;
}

.total-amount {
  font-size: 14px;
  color: #333;
}

.total-amount .amount {
  font-size: 16px;
  color: #ee0a24;
  font-weight: 500;
  margin-left: 4px;
}

.submit-btn {
  width: 120px;
  height: 36px;
  font-size: 14px;
}

.submit-btn[disabled] {
  opacity: 0.6;
  cursor: not-allowed;
}
</style>